﻿using System;
using System.Collections.Generic;

namespace BJoin.Algo
{
    public static class DivisionHelper
    {
        /// <summary>
        /// 平均分配
        /// </summary>
        /// <param name="total">总数</param>
        /// <param name="num">分配数</param>
        /// <returns></returns>
        public static IEnumerable<(int, long)> Average(long total, int num)
        {
            Verify(total, num);
            long divisor = (long)Math.Floor((float)total / num);
            long remainder = total % num;
            for (int i = 0; i < num; i++)
            {
                if (remainder > 0 && i < remainder)
                    yield return (i, divisor + 1);
                else
                    yield return (i, divisor);

            }
        }
        static void Verify(long total, int num)
        {
            if (total < num) throw new ArgumentException("the total must great or equal than the part");
        }

    }
}
